package net.chensee.avastxa.logic.handler.resolve;

import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import net.chensee.avastxa.common.exception.HttpRequestResolverException;

public abstract class AbstractRequestResolver implements RequestResolver {

    private RequestResolver next = null;

    @Override
    public Object resolver(HttpRequest request, HttpContent content, Object data) throws HttpRequestResolverException {
        Object result = this.resolver0(request, content, data);
        if (next != null) {
            return next.resolver(request, content, result);
        }
        return result;
    }

    public abstract Object resolver0(HttpRequest request, HttpContent content, Object data) throws HttpRequestResolverException;
    @Override
    public RequestResolver next() {
        return next;
    }
    @Override
    public void setLast(RequestResolver last) {
        RequestResolver r = this;
        while(true) {
            if (r.next() == null) {
                break;
            } else {
                r = r.next();
            }
        }
        r.setLast(last);
    }

}
